package com.csy.rabbit.merchant.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csy.rabbit.commit.result.Result;
import com.csy.rabbit.merchant.entity.Merchant;
import com.csy.rabbit.merchant.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商家表 前端控制器
 * </p>
 *
 * @author csy
 * @since 2022-12-09
 */
@RestController
@RequestMapping("/merchant")
public class MerchantController {

    @Autowired
    private MerchantService merchantService;

    @RequestMapping("method/{str}")
    public void method(@PathVariable String  str){
        System.out.println(str);
    }

    /**
     * 注册
     * @param merchant
     * @return
     */
    @PostMapping("")
    public Result<Result<String>> add(@RequestBody Merchant merchant){
        Result<String> add = merchantService.add(merchant);
        merchant.setPassword("");
        return Result.success(add);
    }

    /**
     * 根据id查询
     * @param merchantId
     * @return
     */
    @GetMapping("{merchantId}")
    public Result<Result<String>> findByMerchantId(@PathVariable String merchantId) {
        Result<String> byMerchantId = merchantService.findByMerchantId(merchantId);
        return Result.success(byMerchantId);
    }

    @GetMapping("findMerchants")
    public Result<List<Merchant>> findMerchants(){
        //可以根据实际情况来使用带参数的list方法
        List<Merchant> merchants = merchantService.list();
        return Result.success(merchants);
    }

    @GetMapping("findMerchantPage/{currentIndex}/{size}")
    public Result<Page<Merchant>> findMerchantPage(@PathVariable int currentIndex,@PathVariable int size){
        Page<Merchant> pageInfo=new Page<>(currentIndex,size);
        return Result.success(merchantService.page(pageInfo));
    }

    @DeleteMapping("{merchantId}")
    public Result<Result<String>> deleteById(@PathVariable String merchantId){
        return Result.success(merchantService.deleteById(merchantId));
    }
}
